package com.renwei.yunlong.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wangqi on 2017/9/29.
 */

public class RoundedRectProgressBar extends View {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int barColor = Color.parseColor("#689DF8");
    private int backColor = Color.parseColor("#CED5E0");
    private int textColor = Color.WHITE;
    private int textBgColor = Color.parseColor("#333333");
    private float radius;

    int progress = 0;
    int maxProgress = 0;

    public RoundedRectProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public RoundedRectProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundedRectProgressBar(Context context) {
        this(context, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        radius = this.getMeasuredHeight() / 10;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //背景
        mPaint.setColor(backColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(new RectF(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight()), radius, radius, mPaint);
        //进度条
        mPaint.setColor(barColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(new RectF(0, 0, maxProgress == 0 ? 0 : (this.getMeasuredWidth() * progress / maxProgress), this.getMeasuredHeight()), radius, radius, mPaint);
        //进度
        mPaint.setColor(textColor);
        mPaint.setTextSize(this.getMeasuredHeight() / 1.5f);
        float x = maxProgress == 0 ? 0 : (this.getMeasuredWidth() * progress / maxProgress - 10);
        float y = this.getMeasuredHeight() / 2f - mPaint.getFontMetrics().ascent / 2f - mPaint.getFontMetrics().descent / 2f;
        if (progress > maxProgress / 2) {
            canvas.drawText(progress + "", this.getMeasuredWidth() * 3 / 4 - 10, y, mPaint);
        } else if (progress < maxProgress / 10) {
            canvas.drawText(progress + "", this.getMeasuredWidth() / 10 - 10, y, mPaint);
        } else {
            canvas.drawText(progress + "", x, y, mPaint);
        }

        mPaint.setColor(textBgColor);
        mPaint.setTextAlign(Paint.Align.RIGHT);
        mPaint.setTextSize(this.getMeasuredHeight() / 1.5f);
        if (progress != 0) {
            canvas.drawText((maxProgress == 0 ? "0" : new DecimalFormat(".0").format((float) (progress * 100) / (float) maxProgress)) + "%", getMeasuredWidth(), y, mPaint);
        }
    }

    /*设置进度条进度*/
    private void setProgress(int progress) {
        if (progress > maxProgress) {
            this.progress = maxProgress;
        } else if (progress < 0) {
            this.progress = 0;
        } else {
            this.progress = progress;
        }
        postInvalidate();
    }

    public void showProgress(final int count, final int maxProgress) {
        progress = 0;
        this.maxProgress = maxProgress;
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                setProgress(progress);
                if (progress >= count) {
                    timer.cancel();
                } else {
                    progress++;
                }
            }
        }, 0, 5);
    }
}
